<?php
namespace Home\Controller;

use Think\Controller;

class ArticleController extends Controller
{
    public function index()
    {
        $articles = D('Article')->order('id desc')->select();
        foreach ($articles as $key => $article) {
            $sort_data = D('Sort')->getSortById($article['sort_id']);
            $articles[$key]['sort'] = $sort_data[0]['name'];
            $user_data = (D('User')->getUserById($article['author_id']));
            $articles[$key]['author'] = $user_data[0]['username'];
            $comment_data = (D('Comment')->getCommentByArticleIdDesc($articles[$key]['id']));
            $articles[$key]['last_comment'] = $comment_data[0]['content'];
            $articles[$key]['last_comment_user_id'] = $comment_data[0]['from_id'];
            $last_comment_user = (D('User')->getUserById($comment_data[0]['from_id']));
            $articles[$key]['last_comment_user_name'] = $last_comment_user[0]['username'];
            $avatar_data = D('Avatar')->getAvatarById($user_data[0]['id']);
            $articles[$key]['avatar_name'] = $avatar_data[0]['name'];
        }
        $this->assign('articles', $articles);
        $this->display(T('Home@Article/index'));
    }


    public function add()
    {
        if(!D('User')->checklogin()) return $this->error('请先登录',U('User/login'),1);
        if (IS_POST) {
            $sort = D('Sort')->getSortByName($_POST['sort']);
            $author = D('User')->getUserByUsername($_SESSION['uid']);
            $data['sort_id'] = $sort[0]['id'];
            $data['author_id'] = $author[0]['id'];
            $data['title'] = $_POST['title'];
            $data['content'] = $_POST['content'];
            $data['content']=nl2br(str_replace(' ' , '&nbsp;',$_POST['content']));
            if (D('Article')->data($data)->add()) {
                return $this->success('添加成功', U('Article/index'), 1);
            } else {
                return $this->error('添加失败', U('Article/add'), 1);
            }
        }

        $sorts = D('Sort')->getAllSort();
        $this->assign('sorts', $sorts);
        $this->display(T('Home@Article/add'));
    }

    public function edit()
    {
        if(!D('User')->checklogin()) return $this->error('请先登录',U('User/login'),1);
        //必须先判断是为文章所有者或者root
        //if(){}
        $article['id'] = $_POST['id'];
        $article['content']=nl2br(str_replace(' ' , '&nbsp;',$_POST['content']));
        $article_id = D('Article')->updateArticleById($article['id'], $article);
        if ($article_id) {
            return $this->success('更新成功', U('Article/detail', array('id' => $article['id'])), 1);
        }
        return $this->error('更新失败', U('Article/detail', array('id' => $article['id'])), 1);
    }

    public function delete()
    {
        if(!D('User')->checklogin()) return $this->error('请先登录',U('User/login'),1);
        //必须先判断是为文章所有者或者root
        $article['article_id'] = $_POST['article_id'];
        $comments = D('Comment')->getCommentByArticleId($article['article_id']);
//        dump($comments);die();
        foreach ($comments as $key => $comment) {
            D('Comment')->deleteCommentById($comment['id']);
        }
        D('Article')->deleteArticleById($article['article_id']);
        return $this->success('删除成功', U('Article/index'), 1);//删除帖子

    }

    public function detail()
    {
        $self_url=$_SERVER['PATH_INFO'];
        $id=explode(".",end(explode("/",$self_url)))['0'];
        $article = D('Article')->getDetailArticleByArticleId($id);
//        dump($self_url);
//        dump($_SERVER);
//        dump($id);die();
        if (!$article) {
            return $this->error('文章不存在或已经删除', U('Article/index'), 1);
        }
        $visit_user = D('User')->getUserByUsername($_SESSION['uid']);
        $visit_user_type = $visit_user[0]['user_type'];
        $comments = D('Comment')->where("article_id='$id'")->select();
        $article_author = D('User')->getUserByArticleId($id);
        foreach ($comments as $key => $Comment) {
            $from_name = D('User')->getUserById($Comment['from_id']);
            $comments[$key]['from_name'] = $from_name[0]['username'];
            $avatar_data = D('Avatar')->getAvatarByUserId($Comment['from_id']);
            $comments[$key]['avatar_name'] = $avatar_data[0]['name'];
            $comments[$key]['article_author'] = $article_author[0]['username'];
            $to_user = D('User')->getUserById($Comment['to_id']);
            if ($Comment['to_type'] == 'user') {
                $comments[$key]['to_id'] = $to_user[0]['id'];
                $comments[$key]['to_name'] = $to_user[0]['username'];
            }
            $comments[$key]['reply_id'] = 'reply_' . $to_user[0]['id'];
            $comments[$key]['delete_id'] = 'delete_' . $to_user[0]['id'];
        }
        $article['delete_id'] = $article['id'];
//        dump($article);die();
        $this->assign('visit_user_type', $visit_user_type);
        $this->assign('article', $article);
        $this->assign('comments', $comments);
        $this->display(T('Home@Article/detail'));
    }
}